package cn.fastoo.common.dao.member;
import cn.fastoo.common.dao.system.AdminSystemDataPermissionDao;
import cn.fastoo.common.dao.system.UserBaseInfoDao;
import cn.fastoo.common.entity.member.Member;
import cn.fastoo.common.entity.system.UserBaseInfo;
import cn.fastoo.common.sampleframe.dao.FastDao;
import cn.fastoo.common.sampleframe.dao.impl.FastDaoImpl;
import cn.fastoo.common.sampleframe.search.Search;
import cn.fastoo.common.sampleframe.springside.Collections3;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.domain.Page;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.sql.DataSource;
import java.util.*;


@Component
@Transactional
public class MemberDao extends FastDaoImpl<Member> implements FastDao<Member> {


    public Logger logger = LoggerFactory.getLogger(MemberDao.class);

    @Autowired
    @Qualifier("mainDS")
    private void setJt(DataSource ds){
        this.jt = new JdbcTemplate(ds);
    }
    @Autowired
    private UserBaseInfoDao userBaseInfoDao;
    @Autowired
    private MemberGradeDao memberGradeDao;
    @Autowired
    private AdminSystemDataPermissionDao dataPermissionDao;


    public void fenpei(String memberIds,Long guwenId){
        try {
            jt.update(" update member set salesUserId = ? , collectTime = NOW() ,yujingFlag=0 "+
                    "where id in ("+memberIds+")",guwenId);
        }catch (Exception e){
            e.printStackTrace();
        }

    }
    public void shifang(List<Member> ids){
        if(Collections3.isEmpty(ids)){
            return;
        }
        List<Object[]> idsArgs =new ArrayList<>();
        ids.forEach(t->{
            idsArgs.add(new Object[]{t.getId()});
        });
        jt.batchUpdate(" update member set salesUserId = 0  where id = ?",idsArgs);
    }
    public void updateYujing(List<Member> ids){
        if(Collections3.isEmpty(ids)){
            return;
        }
        List<Object[]> idsArgs =new ArrayList<>();
        ids.forEach(t->{
            idsArgs.add(new Object[]{t.getId()});
        });
        jt.batchUpdate(" update member set yujingFlag = 1  where id = ?",idsArgs);
    }

    public Page<Member> findPage(Search search,Long userId) {

        dataPermissionDao.addDataPermissionSql(search,userId);

        Page<Member> members =  super.findPage(search);




        if(Collections3.isNotEmpty(members.getContent())){
            /** 填充销售顾问信息和市场顾问信息 **/
            Set<Long> ids  = new HashSet<>();
            members.getContent().forEach(t->{
                ids.add(t.getMarketingUserId());
                ids.add(t.getSalesUserId());
            });
            if(Collections3.isNotEmpty(ids)){
                List<UserBaseInfo> ms = userBaseInfoDao.find(new Search().addSearchFilter("IN_id",ids));
                Map<Long,String> userMapName = new HashMap<Long,String>();
                ms.forEach(t->{
                    userMapName.put(t.getId(),t.getName());
                });
                members.getContent().forEach(t->{
                    if(t.getMarketingUserId()!=0){
                        t.setMarketingUserName(userMapName.get(t.getMarketingUserId()));
                    }
                    if(t.getSalesUserId()!=0){
                        t.setSalesUserName(userMapName.get(t.getSalesUserId()));
                    }
                    /** 填充用户等级 **/
                    t.setMemberGradeName(memberGradeDao.getNameById(t.getMemberGradeId()));
                });
            }
        }
        return members;
    }


    public void insert(Member member,Long userId,String name) {
        member.setAdminSystemOrganId(userBaseInfoDao.getUserOrganId(userId));
        member.setPersonalId(userId);
        member.setCreatedTime(new Date());
        member.setCreatedMan(name);
        super.insert(member);
    }
}
